package jc.controller;

import jc.service.test.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by 金成 on 2015/7/14.
 */

@Controller
@RequestMapping("")
public class SigninController {

    static final Logger logger = LoggerFactory.getLogger(SigninController.class);

    @RequestMapping("")
    public String index(){
        return "redirect:/sign";
    }


    @RequestMapping("/sign")
    public ModelAndView sign(
            @CookieValue(value = "Username", required = false) String cookieUsername,
            @CookieValue(value = "Password", required = false) String cookiePassword,
            @CookieValue(value = "AutoSign", required = false) String cookieAutoSign,
            @CookieValue(value = "SignMethod", required = false) String cookieSignMethod,
            HttpSession session,
            RedirectAttributes redirectAttributes  //重定向带参使用的属性，包含用户名和密码
    ){


        ModelAndView modelAndView = new ModelAndView("sign");
        Boolean sessionLogin = (Boolean)session.getAttribute("signin");

        //存在session，与服务器的当前会话还没有结束
        if (sessionLogin != null && sessionLogin){
            modelAndView.setViewName("redirect:/proxy");
            return modelAndView;
        }

        //不存在session，但存在自动登录的cookie，可以使用cookie中的用户名密码自动登录
        if (cookieUsername != null && cookiePassword != null &&cookieAutoSign != null && cookieSignMethod != null
                                        &&
            !"".equals(cookieUsername) && !"".equals(cookiePassword) && "remember-me".equals(cookieAutoSign) && "cookie".equals(cookieSignMethod)
            ) {

            modelAndView.setViewName("redirect:/signin");
            redirectAttributes.addFlashAttribute("inputUsername", cookieUsername);
            redirectAttributes.addFlashAttribute("inputPassword", cookiePassword);
            redirectAttributes.addFlashAttribute("inputAutoSign", cookieAutoSign);
            redirectAttributes.addFlashAttribute("inputSignMethod", cookieSignMethod);

            return modelAndView;


        }

        return modelAndView;
    }


    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/signin")
    public ModelAndView signin(
            @ModelAttribute(value = "inputUsername") String inputUsername,
            @ModelAttribute(value = "inputPassword") String inputPassword,
            @ModelAttribute(value = "inputAutoSign") String inputAutoSign,
            @ModelAttribute(value = "inputSignMethod") String inputSignMethod,
            HttpSession session,
            HttpServletResponse response
    ){

        ModelAndView modelAndView = new ModelAndView("signresult");


        if (inputUsername == null || inputPassword == null){
            modelAndView.setViewName("redirect:/sign");
            return modelAndView;
        }



        boolean loginResult = userService.login(inputUsername, inputPassword);

        if (loginResult){
            modelAndView.addObject("result", "登陆成功");
            modelAndView.addObject("signMethod", inputSignMethod);
            session.setAttribute("signin", true);
            session.setMaxInactiveInterval(60 * 60);

            int expireTime = 3600*24*7;

            Cookie cookieUsername = new Cookie("Username", inputUsername);
            cookieUsername.setMaxAge(expireTime);
            Cookie cookiePassword = new Cookie("Password", inputPassword);
            cookiePassword.setMaxAge(expireTime);
            Cookie cookieAutoSign = new Cookie("AutoSign", null);
            cookieAutoSign.setMaxAge(expireTime);
            Cookie cookieSignMethod = new Cookie("SignMethod", "cookie");
            cookieSignMethod.setMaxAge(expireTime);

            if (inputAutoSign != null && "remember-me".equals(inputAutoSign)){
                cookieAutoSign.setValue("remember-me");
            }
            else{
                cookieAutoSign.setValue("forget-me");
            }

            response.addCookie(cookieUsername);
            response.addCookie(cookiePassword);
            response.addCookie(cookieAutoSign);
            response.addCookie(cookieSignMethod);



        }
        else{
            modelAndView.addObject("result", "登录失败");
        }

        return modelAndView;
    }

    @RequestMapping("/signoff")
    public ModelAndView signoff(
            HttpSession session,
            HttpServletResponse response
    ){

        ModelAndView modelAndView = new ModelAndView("redirect:/sign");

        //销毁session
        session.removeAttribute("signin");

        //销毁cookie
        Cookie cookieUsername = new Cookie("Username", null);
        cookieUsername.setMaxAge(0);
        Cookie cookiePassword = new Cookie("Password", null);
        cookiePassword.setMaxAge(0);
        Cookie cookieAutoSign = new Cookie("AutoSign", null);
        cookieAutoSign.setMaxAge(0);
        Cookie cookieSignMethod = new Cookie("SignMethod", null);
        cookieSignMethod.setMaxAge(0);

        response.addCookie(cookieUsername);
        response.addCookie(cookiePassword);
        response.addCookie(cookieAutoSign);
        response.addCookie(cookieSignMethod);

        return modelAndView;
    }

    @RequestMapping("/destroysession")
    public ModelAndView destroysession(HttpSession session){

        ModelAndView modelAndView = new ModelAndView("redirect:/sign");

        session.removeAttribute("signin");

        return  modelAndView;
    }




}
